package silo.dungeonbuddy;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;

public class RunnableClient implements Runnable
{
	private String returnMessage = "DEFAULT";
	private String receivedMessage = "DEFAULT";
	private Start game;
	private Socket clientSocket=null;
	private long callTime = System.currentTimeMillis();

    public RunnableClient(Start eGame,Socket clientSocket)
    {
        this.clientSocket = clientSocket;
		game=eGame;
		returnMessage="!MAP";
	}//constructor

    public void run()
    {
    	if((System.currentTimeMillis()-callTime)>200)
    	{
    		try
    		{
    			clientSocket.setSoTimeout(500);
    			DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
    			BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    			outToServer.writeBytes(returnMessage + '\n');
    			receivedMessage = inFromServer.readLine();
    			System.out.println("FROM SERVER: " + receivedMessage);
    			clientSocket.close();
    		}catch(Exception e)
    		{
    			
    		}//catch
    		try
    		{
    			if(returnMessage.contains("!MAP"))
    			{
    				InputMessage IM = new InputMessage(receivedMessage,game);
    				IM.buildMap();
    				String mess="";
    				String userName=game.getUserName();
    				Unit u = null;
    				for(int i=0;i<game.getUnitList().size();i++)
    				{
    					try
    					{
    						u = (Unit)game.getUnitList().get(i);
    						if(u.getOwner().equalsIgnoreCase(userName))
    						{
    							mess = u.toString();
    							i = game.getUnitList().size();
    						}
    					}catch(Exception e)
    					{
    						mess="!NO UNIT FROM PLAYER,";
    					}//catch
    				}//next i
    				if(u==null)
    				{
    					mess="!NO UNIT FROM PLAYER,";
    				}
    				returnMessage = "!UPDATE," + userName + "," + mess;
    			}else if(returnMessage.contains("!UPDATE"))
    			{
    				InputMessage IM = new InputMessage(receivedMessage,game);
    				IM.update();
    				String mess="";
    				String userName=game.getUserName();
    				Unit u = null;
    				for(int i=0;i<game.getUnitList().size();i++)
    				{
    					try
    					{
    						u = (Unit)game.getUnitList().get(i);
    						if(u.getOwner().equalsIgnoreCase(userName))
    						{
    							mess = u.toString();
    							i = game.getUnitList().size();
    						}
    					}catch(Exception e)
    					{
    						mess="!NO UNIT FROM PLAYER,";
    					}//catch
    				}//next i
    				if(u==null)
    				{
    					mess="!NO UNIT FROM PLAYER,";
    				}
    				returnMessage = "!UPDATE," + userName + "," + mess;
    			}
    		}catch(Exception e)
    		{
    			System.out.println("ERROR!!  CODE 7" + e.getMessage());
    		}//catch
    		callTime = System.currentTimeMillis();
    	}//if((System.currentTimeMillis()-callTime)>200)
    }// Run
    
  //GETTERS
	public String getReturnMessage()
	{
		return returnMessage;
	}
	public String getReceivedMessage()
	{
		return receivedMessage;
	}
	
	//SETTERS
	public void setReturnMessage(String eMess)
	{
		returnMessage=eMess;
	}
	public void setReceivedMessage(String eMess)
	{
		receivedMessage=eMess;
	}
}//class RunnableServer